package com.allen.mk.admin.controller;

import com.allen.mk.admin.service.impl.SysRoleServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import page.CommonPage;
import request.SysRoleRequest;
import response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import request.PageParamRequest;
import response.SysRoleResponse;
import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Allen
 * @since 2024-04-11
 */
@RestController
@RequestMapping("/sys-role")
public class SysRoleController {

    @Resource
    private SysRoleServiceImpl sysRoleService;


    @GetMapping("/roleList")
    @ApiOperation("角色分页列表")
    public Result<CommonPage<SysRoleResponse>> pageRoles(@Validated PageParamRequest request){
        return Result.success(CommonPage.restPage(sysRoleService.getList(request)));
    }

    @PostMapping("/addRole")
    @ApiOperation("添加角色")
    @PreAuthorize("hasAnyAuthority('管理员')")
    public Result<SysRoleResponse> save(@RequestBody SysRoleRequest sysRoleRequest){
        if (sysRoleService.saveRole(sysRoleRequest) == true){
            return Result.success("添加成功");
        }else {
            return Result.failed("添加失败");
        }
    }

    @PostMapping("/deleteRole")
    @ApiOperation("删除角色")
    public Result<SysRoleRequest> delete(@RequestParam Integer id){
        if (sysRoleService.deleteRole(id)){
            return Result.success("删除成功");
        }else {
            return Result.failed("删除失败");
        }
    }

    @PostMapping("/selectRole")
    @ApiOperation("按名称查找角色")
    public Result<SysRoleResponse> select(@RequestParam String rolename){
        return Result.success(sysRoleService.selectRole(rolename));
    }

    @PostMapping("/updateRole")
    @ApiOperation("编辑角色")
    public Result<SysRoleResponse> update(@RequestBody SysRoleRequest request){
        if (sysRoleService.updateRole(request) == true){
            return Result.success("编辑成功");
        }else {
            return Result.failed("编辑失败");
        }
    }
}
